package com.xuxiao.controller;

import com.xuxiao.common.Result;
import com.xuxiao.common.ReturnUpPa;
import com.xuxiao.entrty.UserInfo;
import com.xuxiao.service.UserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;

    @GetMapping("/name")
    public Result getByUsernameIsSame(@RequestParam String username) {
        log.info("查询是否存在：{}", username);
        int result = userInfoService.getByUsernameIsSame(username);
        return result == 0 ? Result.success() : Result.error("用户名已存在");
    }

    /*
    查询所有用户
     */
    @GetMapping("/list")
    public Result getAllUserInfomation() {
        log.info("查询所有用户");
        List<UserInfo> list = userInfoService.getAllUserInfomation();
        return Result.success(list);
    }

    /*
    更改用户信息
     */
    @PutMapping()
    public Result updateUserByCustomerID(@RequestBody UserInfo userInfo) {
        log.info("更改用户信息：userinfo:{}", userInfo);
        userInfoService.updateUserByCustomerID(userInfo);
        return Result.success();
    }

    /*
    按ID查询
     */
    @GetMapping("/{id}")
    public Result GetUserByID(@PathVariable String id) {
        log.info("按ID查询用户,id:{}", id);
        UserInfo userInfo = userInfoService.GetUserByID(id);
        return Result.success(userInfo);
    }

    /*
    删除用户
     */
    @DeleteMapping()
    public Result DeleteByID(@RequestBody List<Integer> ids) {
        log.info("按ID删除用户,id:{}", ids);
        userInfoService.DeleteByID(ids);
        return Result.success();
    }

    @GetMapping("/name/{name}")
    public Result SelectByName(@PathVariable String name) {
        log.info("按姓名查询，{}", name);
        List<UserInfo> userInfoList = userInfoService.SelectByName(name);
        return Result.success(userInfoList);
    }

}
